package com.zhoug.adcommon3demo.fileselector;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.zhoug.adcommon3demo.MyBaseActivity;
import com.zhoug.adcommon3demo.R;
import com.zhoug.common3.Logger;
import com.zhoug.common3.camera.ui.CaptureImageActivity;
import com.zhoug.common3.fileselector.FilePreview;
import com.zhoug.common3.fileselector.FileSelectorHelper;
import com.zhoug.common3.fileselector.beans.LocalMedia;
import com.zhoug.common3.fileselector.selector.FileSelectedConfig;
import com.zhoug.common3.fileselector.selector.FileSelector;
import com.zhoug.common3.fileselector.selector.FileSelectorBuilder;
import com.zhoug.common3.fileselector.selector.FileSelectorDialog;
import com.zhoug.common3.fileselector.selector.IFileSelector;
import com.zhoug.common3.fileselector.selector.OnCaptureClick;
import com.zhoug.common3.permission.ZPermissions;
import com.zhoug.common3.utils.UriUtils;

import java.util.List;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;

/**
 * 测试文件选择器
 * @Author: zhoug
 * @Date: 2025-02-08
 * @Description:
 */
public class TestFileSelectorActivity extends MyBaseActivity {
    private static final String TAG = ">>>TestFileSelectorActivit";
    private  ActivityResultLauncher<Intent> mCaptureImgLauncher;
    private IFileSelector mIFileSelector;
    private FileSelectorDialog mFileSelectorDialog;

    private Button mBtn1;
    private Button mBtn2;
    private TextView mTvResult;
    private FilePreview mFilePreview;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_test_file_selector;
    }

    @Override
    protected void findViews() {

        mBtn1 = findViewById(R.id.btn1);
        mBtn2 = findViewById(R.id.btn2);
        mTvResult = findViewById(R.id.tv_result);
        mFilePreview = findViewById(R.id.filePreview);
    }

    @Override
    protected void onCreate1(Bundle savedInstanceState) {
        getLifecycle().addObserver(new DefaultLifecycleObserver() {
            @Override
            public void onCreate(@NonNull LifecycleOwner owner) {
                mCaptureImgLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        if(result!=null && result.getResultCode()==Activity.RESULT_OK && result.getData()!=null){
                            Intent data = result.getData();
                            Uri uri = data.getData();
                            if(uri!=null){
                                String path = UriUtils.getPathFromUri(getActivity(), uri);
                                Logger.d(TAG, "自定义拍照 path:"+path);
                            }
                        }
                    }
                });
            }
            @Override
            public void onDestroy(@NonNull LifecycleOwner owner) {
                mCaptureImgLauncher.unregister();
            }
        });


        FileSelectorHelper.setOnCaptureClick(new OnCaptureClick() {
            @Override
            public boolean image() {

                return true;
            }

            @Override
            public boolean audio() {
                return false;
            }

            @Override
            public boolean video() {
                return false;
            }

            @Override
            public void captureImage(Activity activity, int requestCode, String path) {
                Intent imageIntent=new Intent(getActivity(),CaptureImageActivity.class);
                imageIntent.putExtra(CaptureImageActivity.EXTRA_PATH,path);
                imageIntent.putExtra(CaptureImageActivity.EXTRA_PIC_WIDTH,1080);
                imageIntent.putExtra(CaptureImageActivity.EXTRA_PIC_HEIGHT,1920);
                mCaptureImgLauncher.launch(imageIntent);
            }

            @Override
            public void captureVideo(Activity activity, int requestCode, String path) {

            }

            @Override
            public void captureAudio(Activity activity, int requestCode, String path) {

            }
        });


        mFilePreview.setDeleteEnable(true);
        FileSelectorBuilder builder = new FileSelectorBuilder(this);
        mIFileSelector = builder.captureAll(true)
                .compressImage(true)
                .captureAudio(true)
                .captureVideo(true)
                .captureImage(true)
                .singleModel(false)
                .setMaxNum(5)
                .setOnSelectListener((FileSelector.onSelectListener) this::onSelectFileResult)
                .build();

        mBtn1.setOnClickListener(v -> {
            ZPermissions.with(this)
                    .setPermissions(Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .setCallback((success, granted, denied) -> {
                        mIFileSelector.show();
                    })
                    .request();

        });

        mFileSelectorDialog=new FileSelectorDialog(this);
        FileSelectedConfig fileSelectedConfig = mFileSelectorDialog.getFileSelectedConfig();
        fileSelectedConfig.captureAudio=true;
        fileSelectedConfig.captureImage=true;
        fileSelectedConfig.captureVideo=true;
        mFileSelectorDialog.setOnSelectListener((FileSelector.onSelectListener) this::onSelectFileResult);
        mBtn2.setOnClickListener(v->{
            mFileSelectorDialog.show();

        });
    }

    private void onSelectFileResult(List<LocalMedia> medias){
        if (medias != null && !medias.isEmpty()) {
            StringBuilder stringBuilder = new StringBuilder();
            for (LocalMedia item : medias) {
                stringBuilder.append(item.getPath());
                stringBuilder.append("\n");
            }
            mTvResult.setText(stringBuilder.toString());
            mFilePreview.addFiles(medias);
        }else{
            mTvResult.setText(null);
            mFilePreview.addFiles(null);
        }
    }


   /* @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (mIFileSelector != null) {
            mIFileSelector.onActivityResult(requestCode, resultCode, data);
        }
        if (mFileSelectorDialog != null) {
            mFileSelectorDialog.onActivityResult(requestCode, resultCode, data);
        }
    }*/

}
